//
//  UIImageView+JwExten.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-21.
//

#import "UIImageView+JwExten.h"
#import <objc/runtime.h>
#import "JwCate.h"

NSString *GV_UIImageViewJwExten_holdClassName = @"JwImageHoldView";

NSString *GV_JwImageHoldView_backColorString = @"AFAFAF";
NSString *GV_JwImageHoldView_holdImgName = @"jw_hold_loadfailed";

@implementation JwImageHoldView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kJwColorHexString(GV_JwImageHoldView_backColorString);
        self.holdImgV = [[UIImageView alloc] init];
        self.holdImgV.contentMode = UIViewContentModeScaleAspectFit;
        self.holdImgV.image = [UIImage imageNamed:GV_JwImageHoldView_holdImgName];
        [self addSubview:self.holdImgV];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat w = self.jw_width * 0.6;
    CGFloat h = self.jw_height * 0.6;
    CGFloat v = w > h ? h : w;
    self.holdImgV.frame = CGRectMake(0, 0, v, v);
    self.holdImgV.center = self.center;
}

- (void)setupValue:(id)value {
    
}

@end

@implementation UIImageView (JwExten)

- (JwImageHoldView *)jw_imageHoldView {
    return objc_getAssociatedObject(self, @selector(jw_imageHoldView));
}

- (void)setJw_imageHoldView:(JwImageHoldView *)jw_imageHoldView {
    objc_setAssociatedObject(self, @selector(jw_imageHoldView), jw_imageHoldView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString placeImage:(UIImage *)placeImage holdImage:(UIImage *)holdImage completed:(SDExternalCompletionBlock)completed {
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    
    if ([self.subviews containsObject:self.jw_imageHoldView]) {
        [self.jw_imageHoldView removeFromSuperview];
        self.jw_imageHoldView = nil;
    }
    if (!placeImage) {
        Class holdClass = NSClassFromString(GV_UIImageViewJwExten_holdClassName);
        self.jw_imageHoldView = [[holdClass alloc] init];
        [self addSubview:self.jw_imageHoldView];
        [self.jw_imageHoldView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.offset(0);
        }];
        if (holdImage) {
            self.jw_imageHoldView.holdImgV.image = holdImage;
        }
    }
    
    [self sd_setImageWithURL:url placeholderImage:placeImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (image && self.jw_imageHoldView) {
            [self.jw_imageHoldView removeFromSuperview];
            self.jw_imageHoldView = nil;
        }
        if (completed) {
            completed(image, error, cacheType, imageURL);
        }
    }];
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString {
    [self jw_sd_setImageWithUrlString:urlString placeImage:nil holdImage:nil completed:nil];
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString holdImage:(UIImage *)holdImage {
    [self jw_sd_setImageWithUrlString:urlString placeImage:nil holdImage:holdImage completed:nil];
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString completed:(SDExternalCompletionBlock)completed {
    [self jw_sd_setImageWithUrlString:urlString placeImage:nil holdImage:nil completed:completed];
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString placeImage:(UIImage *)placeImage {
    [self jw_sd_setImageWithUrlString:urlString placeImage:placeImage holdImage:nil completed:nil];
}

- (void)jw_sd_setImageWithUrlString:(NSString *)urlString value:(id)value completed:(SDExternalCompletionBlock)completed {
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlString];
    
    if ([self.subviews containsObject:self.jw_imageHoldView]) {
        [self.jw_imageHoldView removeFromSuperview];
        self.jw_imageHoldView = nil;
    }

    Class holdClass = NSClassFromString(GV_UIImageViewJwExten_holdClassName);
    self.jw_imageHoldView = [[holdClass alloc] init];
    [self addSubview:self.jw_imageHoldView];
    [self.jw_imageHoldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.offset(0);
    }];
    [self.jw_imageHoldView setupValue:value];
    
    [self sd_setImageWithURL:url placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (image && self.jw_imageHoldView) {
            [self.jw_imageHoldView removeFromSuperview];
            self.jw_imageHoldView = nil;
        }
        if (completed) {
            completed(image, error, cacheType, imageURL);
        }
    }];
}

@end
